/* 
 * File:   addTaskTempDialog.cpp
 * Author: nicolas
 * 
 * Created on 16 février 2013, 18:05
 */

#include "addTaskTempDialog.h"
#include "modele/Task.h"
#include <sstream>
#include <string.h>

/*Popup appelée en cas d'ajout d'une nouvelle tache*/
addTaskTempDialog::addTaskTempDialog(accueil* parent) {
    widget.setupUi(this);
    parent_=parent;
    
     /*Initialisation des champs de la popup*/
    for(int i=1; i<10; i++){
        ostringstream os;
        os << i;
        string res = os.str();
        widget.comboBoxNumTacheOrdo->addItem(res.c_str());
    }
    
    for(int i=1; i<7; i++){
        ostringstream os;
        os << i;
        string res = os.str();
        if(i==1){
            widget.comboBoxTacheRelative->addItem((res+" jour avant").c_str()); 
        }else{
            widget.comboBoxTacheRelative->addItem((res+" jours avant").c_str());
        }  
    }
    for(int i=1; i<4; i++){
        ostringstream os;
        os << i;
        string res = os.str();
        if(i==1){
            widget.comboBoxTacheRelative->addItem((res+" semaine avant").c_str()); 
        }else{
            widget.comboBoxTacheRelative->addItem((res+" semaines avant").c_str());
        }  
    }
    for(int i=1; i<4; i++){
        ostringstream os;
        os << i;
        string res = os.str();
        widget.comboBoxTacheRelative->addItem((res+" mois avant").c_str());
    }
    
    widget.radioButtonTacheFixe->setDisabled(true);
    widget.radioButtonTacheRelative->setDisabled(true);
    widget.comboBoxNumTacheOrdo->setDisabled(true);
    widget.dateEditTacheFixe->setDisabled(true);
    widget.comboBoxTacheRelative->setDisabled(true);
    widget.pushButtonValidTache->setDisabled(true);
    /************************************************/
    
     /*Connexion des widgets*/
    QObject::connect(widget.pushButtonValidTache, SIGNAL(clicked()), this, SLOT(addTaskListener()));
    QObject::connect(widget.checkBoxEcheanceTache, SIGNAL(clicked()), this, SLOT(radioButtonCheckableListener()));
    QObject::connect(widget.radioButtonTacheOrdoY, SIGNAL(clicked()), this, SLOT(comboBoxListener()));
    QObject::connect(widget.radioButtonTacheOrdoN, SIGNAL(clicked()), this, SLOT(comboBoxListener()));
    QObject::connect(widget.radioButtonTacheFixe, SIGNAL(clicked()), this, SLOT(dateEditListener())); 
    QObject::connect(widget.radioButtonTacheRelative, SIGNAL(clicked()), this, SLOT(dateEditListener()));
    QObject::connect(widget.pushButtonFermer, SIGNAL(clicked()), this, SLOT(closePopupListener()));
    QObject::connect(widget.radioButtonTacheRelative, SIGNAL(clicked()), this, SLOT(comboBoxRelativeListener()));
    QObject::connect(widget.listTaskName, SIGNAL(textChanged(QString)), this, SLOT(boutonValidListener()));
    /***************************/
}
/*Popup appelée en cas de modification d'une tache*/
addTaskTempDialog::addTaskTempDialog(accueil* parent, Task *task){
    widget.setupUi(this);
    parent_=parent;
    task_=task;
     /*Initialisation des champs de la popup*/
    for(int i=1; i<10; i++){
        ostringstream os;
        os << i;
        string res = os.str();
        widget.comboBoxNumTacheOrdo->addItem(res.c_str());
    }
    for(int i=1; i<7; i++){
        ostringstream os;
        os << i;
        string res = os.str();
        if(i==1){
            widget.comboBoxTacheRelative->addItem((res+" jour avant").c_str()); 
        }else{
            widget.comboBoxTacheRelative->addItem((res+" jours avant").c_str());
        }  
    } 
    for(int i=1; i<4; i++){
        ostringstream os;
        os << i;
        string res = os.str();
        if(i==1){
            widget.comboBoxTacheRelative->addItem((res+" semaine avant").c_str()); 
        }else{
            widget.comboBoxTacheRelative->addItem((res+" semaines avant").c_str());
        }  
    }
    for(int i=1; i<4; i++){
        ostringstream os;
        os << i;
        string res = os.str();
        widget.comboBoxTacheRelative->addItem((res+" mois avant").c_str());
    }
   
    string nomTask = task_->getTitre();
    widget.listTaskName->setText(QString(nomTask.c_str()));
    
    bool ordoTask = task_->isOrdo();
    widget.radioButtonTacheOrdoN->setChecked(!ordoTask);
    widget.radioButtonTacheOrdoY->setChecked(ordoTask);
    if(widget.radioButtonTacheOrdoN->isChecked()){
        widget.comboBoxNumTacheOrdo->setDisabled(true);
    }
    else{
        widget.comboBoxNumTacheOrdo->setDisabled(false);
    }

    int num = task_->getNumber();
    widget.comboBoxNumTacheOrdo->setCurrentIndex(num-1); 
    
    bool echeance = task_->isEcheance();
    widget.checkBoxEcheanceTache->setChecked(echeance);
    if(widget.checkBoxEcheanceTache->isChecked()){
        widget.radioButtonTacheFixe->setDisabled(false);
        widget.radioButtonTacheRelative->setDisabled(false);
        
        bool fixe = task_->isFixe();
        widget.radioButtonTacheFixe->setChecked(fixe);
        widget.radioButtonTacheRelative->setChecked(!fixe);
        if(widget.radioButtonTacheFixe->isChecked()){
            widget.dateEditTacheFixe->setDisabled(false);
            widget.comboBoxTacheRelative->setDisabled(true);
        }
        else{
            widget.dateEditTacheFixe->setDisabled(true);
            widget.comboBoxTacheRelative->setDisabled(false);
        }
    }else{
        widget.radioButtonTacheFixe->setDisabled(true);
        widget.radioButtonTacheRelative->setDisabled(true);
        widget.dateEditTacheFixe->setDisabled(true);
        widget.comboBoxTacheRelative->setDisabled(true);
    }

    int numRelat = task_->getRelative();
    widget.comboBoxTacheRelative->setCurrentIndex(numRelat); 
    Date d = task_->getDate();
    QDate date(d.getYear(),d.getMonth(),d.getDay());
    widget.dateEditTacheFixe->setDate(date);
     /************************************/  
    
    /*Connexion des widgets*/
    QObject::connect(widget.checkBoxEcheanceTache, SIGNAL(clicked()), this, SLOT(radioButtonCheckableListener()));
    QObject::connect(widget.radioButtonTacheOrdoY, SIGNAL(clicked()), this, SLOT(comboBoxListener()));
    QObject::connect(widget.radioButtonTacheOrdoN, SIGNAL(clicked()), this, SLOT(comboBoxListener()));
    QObject::connect(widget.radioButtonTacheFixe, SIGNAL(clicked()), this, SLOT(dateEditListener())); 
    QObject::connect(widget.radioButtonTacheRelative, SIGNAL(clicked()), this, SLOT(dateEditListener()));
    QObject::connect(widget.pushButtonValidTache, SIGNAL(clicked()), this, SLOT(modifyTaskListener()));
    QObject::connect(widget.pushButtonFermer, SIGNAL(clicked()), this, SLOT(closePopupListener()));
    QObject::connect(widget.radioButtonTacheRelative, SIGNAL(clicked()), this, SLOT(comboBoxRelativeListener()));
    QObject::connect(widget.listTaskName, SIGNAL(textChanged(QString)), this, SLOT(boutonValidListener()));
    /*************************/

}

/*Listener lancé a la validation de la modification d'une tache*/
void addTaskTempDialog::modifyTaskListener() {

     /*Modification des attributs de l'objet tache par la valeur des champs*/
    task_->setTitre(widget.listTaskName->text().toStdString());
    
    if(widget.radioButtonTacheOrdoN->isChecked()){
        task_->setOrdo(false);
        task_->setNumber(0);
    }
    else{
        task_->setOrdo(true);
        task_->setNumber(widget.comboBoxNumTacheOrdo->currentIndex()+1); //+1?
    }
    
    if(widget.checkBoxEcheanceTache->isChecked()){
        task_->setEcheance(true);
    }
    else{
        task_->setEcheance(false);
    }
    
    if(widget.radioButtonTacheFixe->isChecked()){
        task_->setFixe(true);
        QDate date =widget.dateEditTacheFixe->date();
        Date tmpDate = Date(date.day(),date.month(),date.year());
        task_->setDate(tmpDate);
    }
    else{
        task_->setFixe(false);
        task_->setDate(Date());
    }
    if(widget.radioButtonTacheRelative->isChecked()){
        task_->setFixe(false);
        task_->setRelative(widget.comboBoxTacheRelative->currentIndex());
    }else{
        task_->setRelative(-1);
    }

    if(widget.radioButtonTacheOrdoN->isChecked()){ // non coché
        if(widget.checkBoxEcheanceTache->isChecked()){ // non coché echeance coché
            if(widget.radioButtonTacheFixe->isChecked()){ //  non coché echeance coché fixe coché
                QDate date =widget.dateEditTacheFixe->date();
                Date tmpDate = Date(date.day(),date.month(),date.year());
                parent_->modifyTaskTemp(widget.listTaskName->text().toStdString(), 0, tmpDate,-1);    
                accept();
            }
            else{ // non coché echeance coché relative coché
                 parent_->modifyTaskTemp(widget.listTaskName->text().toStdString(), 0, Date(),widget.comboBoxTacheRelative->currentIndex()); 
                 accept();
            }
        }
        else{ // non coché échéance pas coché
             parent_->modifyTaskTemp(widget.listTaskName->text().toStdString(), 0, Date(),-1);   
             accept();
        }
       
    }
    else{ // oui coché
        if(widget.checkBoxEcheanceTache->isChecked()){ // oui coché echeance coché
             if(widget.radioButtonTacheFixe->isChecked()){ //  oui coché echeance coché fixe coché
                 QDate date =widget.dateEditTacheFixe->date();
                 Date tmpDate = Date(date.day(),date.month(),date.year());
                 parent_->modifyTaskTemp(widget.listTaskName->text().toStdString(), widget.comboBoxNumTacheOrdo->currentIndex()+1, tmpDate, -1); 
                 accept();
            }
             else{ // oui coché echeance coche relative coche
                 parent_->modifyTaskTemp(widget.listTaskName->text().toStdString(), widget.comboBoxNumTacheOrdo->currentIndex()+1, Date(),widget.comboBoxTacheRelative->currentIndex());
                 accept();
             }
        }
        else{ // oui coché echeance non coché
            parent_->modifyTaskTemp(widget.listTaskName->text().toStdString(), widget.comboBoxNumTacheOrdo->currentIndex()+1, Date(),-1);
            accept();
        }
    }
    /*******************************************/
    

}

void addTaskTempDialog::addTaskListener(){
    if(widget.radioButtonTacheOrdoN->isChecked()){ // non coché
        if(widget.checkBoxEcheanceTache->isChecked()){ // non coché echeance coché
            if(widget.radioButtonTacheFixe->isChecked()){ //  non coché echeance coché fixe coché
                QDate date =widget.dateEditTacheFixe->date();
                Date tmpDate = Date(date.day(),date.month(),date.year());
                parent_->addTaskTemp(widget.listTaskName->text().toStdString(), false, 0, true, true, tmpDate, -1)  ;         
            }
            else{ // non coché echeance coché relative coché
                parent_->addTaskTemp(widget.listTaskName->text().toStdString(), false, 0, true, false, Date(), widget.comboBoxTacheRelative->currentIndex())  ;         
            }
        }
        else{ // non coché échéance pas coché
             parent_->addTaskTemp(widget.listTaskName->text().toStdString(), false, 0, false, false, Date(), -1)  ;         
        }
       
    }
    else{ // oui coché
        if(widget.checkBoxEcheanceTache->isChecked()){ // oui coché echeance coché
             if(widget.radioButtonTacheFixe->isChecked()){ //  oui coché echeance coché fixe coché
                 QDate date =widget.dateEditTacheFixe->date();
                 Date tmpDate = Date(date.day(),date.month(),date.year());
                 parent_->addTaskTemp(widget.listTaskName->text().toStdString(),true, widget.comboBoxNumTacheOrdo->currentIndex()+1, true, true, tmpDate,-1)  ;         
            }
             else{ // oui coché echeance coche relative coche
                 parent_->addTaskTemp(widget.listTaskName->text().toStdString(), true, widget.comboBoxNumTacheOrdo->currentIndex()+1, true, false, Date(), widget.comboBoxTacheRelative->currentIndex())  ;         
             }
        }
        else{ // oui coché echeance non coché
            parent_->addTaskTemp(widget.listTaskName->text().toStdString(), true, widget.comboBoxNumTacheOrdo->currentIndex()+1, false, false, Date(),-1)  ;         
        }
    }
        /*Vidage des champs de la popup*/
    widget.listTaskName->setText("");
    widget.radioButtonTacheOrdoY->setChecked(false);
    widget.comboBoxNumTacheOrdo->setDisabled(true);
    widget.radioButtonTacheOrdoN->setChecked(true);
    widget.checkBoxEcheanceTache->setChecked(false);
    widget.radioButtonTacheFixe->setDisabled(true);
    widget.radioButtonTacheFixe->setChecked(false);
    widget.radioButtonTacheRelative->setDisabled(true);
    widget.radioButtonTacheRelative->setChecked(false);
    widget.dateEditTacheFixe->setDisabled(true);
    widget.comboBoxTacheRelative->setDisabled(true);
    widget.pushButtonValidTache->setDisabled(true);
    /************************/
}

/*Methode a l'ecoute des appuis sur le checkBox echeance*/
void addTaskTempDialog::radioButtonCheckableListener(){
    
    /*Definition des contraintes sur les widgets en fonction de l'etat du checkbox*/
    if(widget.checkBoxEcheanceTache->isChecked()){
        widget.radioButtonTacheFixe->setDisabled(false);
        widget.radioButtonTacheRelative->setDisabled(false);
        if(!(widget.radioButtonTacheFixe->isChecked())&& !(widget.radioButtonTacheRelative->isChecked())){
            widget.pushButtonValidTache->setDisabled(true);
        }
    }else{
        widget.pushButtonValidTache->setDisabled(false);
        widget.radioButtonTacheFixe->setDisabled(true);
        widget.radioButtonTacheRelative->setDisabled(true);
        widget.radioButtonTacheFixe->setChecked(false);
        widget.radioButtonTacheRelative->setChecked(false);
        widget.comboBoxTacheRelative->setDisabled(true);
        widget.dateEditTacheFixe->setDisabled(true);
    }
    /************************************************************/
}


/*Methode a l'ecoute des appuis sur le boutonRadio Oui*/
void addTaskTempDialog::comboBoxListener(){
    
    /*Definition des contraintes sur les widgets en fonction de l'etat du boutonRadio*/
    if(widget.radioButtonTacheOrdoY->isChecked()){
        widget.comboBoxNumTacheOrdo->setDisabled(false);
        widget.comboBoxNumTacheOrdo->setCurrentIndex(0);
        if(widget.listTaskName->text()!=""){
            widget.pushButtonValidTache->setDisabled(false);
        }else{
            widget.pushButtonValidTache->setDisabled(true);
        }
    }else{
        widget.comboBoxNumTacheOrdo->setDisabled(true);
        if(widget.listTaskName->text()!=""){
            widget.pushButtonValidTache->setDisabled(false);
        }else{
            widget.pushButtonValidTache->setDisabled(true);
        }
    }
    /*******************************************************/
}

/*Methode a l'ecoute des appuis sur le boutonRadio Relative*/
void addTaskTempDialog::comboBoxRelativeListener(){
    
    /*Definition des contraintes sur les widgets en fonction de l'etat du boutonRadio*/
     if(widget.radioButtonTacheRelative->isChecked()){
        widget.comboBoxTacheRelative->setDisabled(false);
        widget.comboBoxTacheRelative->setCurrentIndex(0);
        if(widget.listTaskName->text()!=""){
            widget.pushButtonValidTache->setDisabled(false);
        }
        
    }else{
        widget.comboBoxTacheRelative->setDisabled(true);
        widget.pushButtonValidTache->setDisabled(true);
    }
    /*********************************************************/
}

/*Methode a l'ecoute des appuis sur le boutonRadio Fixe*/
void addTaskTempDialog::dateEditListener(){
    
    /*Definition des contraintes sur les widgets en fonction de l'etat du boutonRadio*/
    if(widget.radioButtonTacheFixe->isChecked()){
        widget.dateEditTacheFixe->setDisabled(false);
        widget.comboBoxTacheRelative->setDisabled(true);
        if(widget.listTaskName->text()!=""){
            widget.pushButtonValidTache->setDisabled(false);
        }
    }else{
        widget.pushButtonValidTache->setDisabled(true);
        //widget.comboBoxTacheRelative->setDisabled(false);
        widget.dateEditTacheFixe->setDisabled(true);
    }
    /**************************************************************/
}

/*Methode a l'ecoute des appuis sur le bouton poussoire Valider*/
void addTaskTempDialog::boutonValidListener(){
    /*Definition des contraintes sur les widgets en fonction de l'etat du bouton*/
    if(widget.listTaskName->text()!=""){
        widget.pushButtonValidTache->setDisabled(false);
    }
    /*****************************************************/
}


/*Methode a l'ecoute des appuis sur le bouton poussoire Terminer*/
void addTaskTempDialog::closePopupListener(){
    accept(); // fermeture de la popup
}

addTaskTempDialog::~addTaskTempDialog() {
}

